<#assign excludeColumns = ["id","create_time","update_time","create_by","update_by"]>
package ${package.ServiceImpl};


import ${package.Entity}.${table.entityName};
import ${package.Mapper}.${table.mapperName};
import ${package.Service}.${table.serviceName};
import org.springframework.stereotype.Service;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
<#if needController>
import cn.hutool.core.collection.CollUtil;
import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.BeanUtils;
import org.springframework.web.multipart.MultipartFile;
import ${voPackageValue}.${voName};
import ${pagePackageValue}.${pageName};
import ${formPackageValue}.${formName};
import java.io.ByteArrayOutputStream;
import java.util.List;
import java.util.Map;
</#if>

/**
 * @Description: ${table.comment!}服务实现类
 * @Author: ${author}
 * @CreateDate: ${date}
 * @Version: 1.0.0
 * @Company: ${company}
 */
@RequiredArgsConstructor
@Service
@Slf4j
public class ${table.serviceImplName} extends ${superServiceImplClass}<${table.mapperName}, ${table.entityName}> implements ${table.serviceName} {


    private final ${table.mapperName} ${classname}Mapper;
    <#if needController>
    @Override
    public Page<${voName}> getPage(${pageName} page) {
        return ${classname}Mapper.getPage(page);
    }

    @Override
    public ${voName} getDetail(Integer id) {
        ${table.entityName} entity = ${classname}Mapper.selectById(id);
        if(entity==null){
            return null;
        }
        ${voName} vo=new ${voName}();
        BeanUtils.copyProperties(entity,vo);
        return vo;
    }

    @Override
    public boolean addEntity(${formName} form) {
        ${table.entityName} entity=new ${table.entityName}();
        BeanUtils.copyProperties(form,entity);
        return save(entity);
    }

    @Override
    public boolean editEntity(${formName} form) {
        if(form.getId()==null){
            return false;
        }
        ${table.entityName} entity=new ${table.entityName}();
        BeanUtils.copyProperties(form,entity);
        return updateById(entity);
    }

    <#if needImportExport>
    @Override
    public boolean importExcel(MultipartFile file) {
        try {
            // 使用Hutool读取Excel文件
            ExcelReader reader = ExcelUtil.getReader(file.getInputStream());
            List<Map<String, Object>> maps = reader.readAll();
            //TODO 实现入库逻辑
            return true;
        }catch (Exception e){
            log.error("导入EXCEL异常",e);
        }
        return false;
    }

    @Override
    public byte[] exportExcel(${pageName} page) {
        Page<${voName}> pageData = ${classname}Mapper.getPage(page);
        if(CollUtil.isEmpty(pageData.getRecords())){
            return null;
        }
        List<${voName}> data = pageData.getRecords();
        try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
            // 创建一个ExcelWriter对象
            ExcelWriter writer = ExcelUtil.getWriter(true);
            // 设置表头
            <#-- ----------  BEGIN 字段循环遍历  ---------->
            <#list table.fields as field>
            <#if !excludeColumns?seq_contains("${field.name}")>
            <#if field.comment!?length gt 0>
            writer.addHeaderAlias("${field.propertyName}", "${field.comment}");
            </#if>
            </#if>
            </#list>
            <#------------  END 字段循环遍历  ---------->
            // 写入数据
            writer.write(data, true);
            // 输出到字节数组输出流
            writer.flush(outputStream, true);
            // 关闭资源
            writer.close();
            return outputStream.toByteArray();
        } catch (Exception e) {
            log.error("导出文件异常",e);
            throw new RuntimeException("导出文件异常", e);
        }
    }
    </#if>
    </#if>
}
